/*
 * @Author: xiezuobing(948466)[435321508@qq.com] 
 * @Date: 2017-12-18 10:11:54 
 * @Last Modified by: xiezuobing
 * @Last Modified time: 2017-12-27 19:45:43
 */

(function() {
    //复制连接
    clipUrl();
    function clipUrl() {
        $(".clipUrl").each(function() {
            var articleId = $(this).attr('articleId');
            var clipBtnId = $(this).attr('id');
            var clipTarget = $('#copy_target_' + articleId);
            var config = {
                tips: [1, '#1ab394'],
                time: 2000
            }
            var clip = new Clipboard('#' + clipBtnId);
            clip.on('success', function(e) {
                //layer_show_
                layer.tips('复制成功', '#clip_borad_' + articleId, config);
                e.clearSelection();
            });
            clip.on('error', function(e) {
                layer.tips('复制失败', '#clip_borad_' + articleId, config);
                e.clearSelection()
            });
        });
    }

    //删除
    deleteArticle();
    function deleteArticle() {
        $('.delete_article').each(function() {
            $(this).click(function() {
                var articleId = $(this).attr('articleId');
                layer.confirm('您确认删除该条文章吗？', {
                    btn: ['确定', '取消'] //按钮
                }, function() {
                    $.ajax({
                        url: '/admin/article/delete?articleId=' + articleId,
                        dataType: 'json',
                        success: function(data) {
                            toastProp(data)
                        },
                        fail: function(err) {
                            laye.msg(err);
                        }
                    })
                }, function() {});


            });
        });;
    }

    //发布
    togglePublish();
    function togglePublish() {
        $('.publish_article').each(function() {
            $(this).click(function() {
                var articleId = $(this).attr('articleId');
                $.ajax({
                    url: '/admin/article/togglePublish?articleId=' + articleId,
                    dataType: 'json',
                    success: function(data) {
                        toastProp(data);
                    },
                    fail: function(err) {
                        console.log(err);
                    }
                });
            });
        });
    }

    //搜索相关
    searchHandler();
    function searchHandler() {
        var queryArr = [
            $('select[name="orderBy"]'),
            $('select[name="order"]'),
            $('input[name="keyword"]'),
            $('select[name="type"]')
        ];
        var submitButton = $("#submitSearch");
        var options = getQueryObject();
        delete options['pageSize'];
        delete options['pageNo'];
        submitButton.click(function() {
            $(queryArr).each(function() {
                var key = this.attr("name");
                options[key] = this.val();
            })
            window.location.href = getUrlByQuery(options)
        });
        $(document).keydown(function(e) {
            if (e.keyCode == 13) submitButton.click();
        })
    }

}())